﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace MemcachedDemo.Library.Config
{
    [Serializable]
    public class MemcachedSettings
    {
        public MemcachedSettings()
        {
            Failover = true;
            Nagle = true;
        }

        private bool _Disable = false;
        /// <summary>
        /// 禁用
        /// </summary>
        public bool Disable
        {
            get { return _Disable; }
            set { _Disable = value; }
        }


        private string _PoolName = "";
        /// <summary>
        /// Memecached 池名字
        /// </summary>
        public string PoolName
        {
            get { return _PoolName; }
            set { _PoolName = string.IsNullOrEmpty(value) ? "MemcachedPool" : value; }
        }

        private ServerInfoCollection _ServerList = new ServerInfoCollection();
        [XmlArray(ElementName = "Servers")]
        public ServerInfoCollection ServerList
        {
            get { return _ServerList; }
            set { _ServerList = value; }
        }

        private int _InitConnections = 3;
        /// <summary>
        /// 初始化连接数
        /// </summary>
        public int InitConnections
        {
            get { return _InitConnections; }
            set { _InitConnections = value > 0 ? value : 3; }
        }

        private int _MinConnections = 3;
        /// <summary>
        /// 最小连接数
        /// </summary>
        public int MinConnections
        {
            get { return _MinConnections; }
            set { _MinConnections = value > 0 ? value : 3; }
        }

        private int _MaxConnections = 10;
        /// <summary>
        /// 最大连接数
        /// </summary>
        public int MaxConnections
        {
            get { return _MaxConnections; }
            set { _MaxConnections = value > 0 ? value : 10; }
        }

        private long _MaintenanceSleep = 1000 * 5;
        /// <summary>
        /// 维护 Gets or sets the sleep time between runs of the pool maintenance thread.
        /// If set to 0, then the maintenance thread will not be started;
        /// </summary>
        /// <value>sleep time in milliseconds</value>
        public long MaintenanceSleep
        {
            get { return _MaintenanceSleep; }
            set { _MaintenanceSleep = value > 0 ? value : 1000 * 5; }
        }

        private int _SocketTimeout = 1000 * 10;
        /// <summary>
        /// Gets or sets the socket timeout for reads
        /// </summary>
        public int SocketTimeout
        {
            get { return _SocketTimeout; }
            set { _SocketTimeout = value > 0 ? value : 1000 * 10; }
        }

        private int _SocketConnectTimeout = 50;
        /// <summary>
        /// Gets or sets the socket timeout for connects.
        /// </summary>
        public int SocketConnectTimeout
        {
            get { return _SocketConnectTimeout; }
            set { _SocketConnectTimeout = value > 0 ? value : 50; }
        }

        /// <summary>
        /// Gets or sets the failover flag for the pool.
        /// 
        /// If this flag is set to true and a socket fails to connect,
        /// the pool will attempt to return a socket from another server
        /// if one exists.  If set to false, then getting a socket
        /// will return null if it fails to connect to the requested server.
        /// </summary>
        public bool Failover
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the Nagle algorithm flag for the pool.
        /// 
        /// If false, will turn off Nagle's algorithm on all sockets created.
        /// </summary>
        public bool Nagle
        {
            get;
            set;
        }

        private HashingAlgorithm _HashingAlgorithm = HashingAlgorithm.NewCompatibleHash;
        /// <summary>
        /// Gets or sets the hashing algorithm we will use.
        /// </summary>
        public HashingAlgorithm HashingAlgorithm
        {
            get { return _HashingAlgorithm; }
            set { _HashingAlgorithm = value; }
        }

    }
}
